<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:doc="livcos.org/ns/doc"
  xmlns:pm="livcos.org/ns/pm"
  xmlns="http://www.w3.org/1999/XSL/Format"
  exclude-result-prefixes="doc pm">

  <xsl:template match="pm:usecase_overview">
    <table table-layout="fixed" width="100%" margin-left="0.6cm" margin-top="12pt"
      font-size="6pt">
      <table-column column-width="25%" />
      <table-column column-width="55%" />
      <table-column column-width="20%" />
      <table-body>
        <xsl:apply-templates select="//doc:chapter[pm:usecase]" mode="overview"/>
      </table-body>
    </table>
  </xsl:template>
  
  <xsl:template match="doc:chapter[pm:usecase]" mode="overview">
    <table-row>
      <table-cell number-columns-spanned="3" margin-left="0cm" padding="1pt" border-style="solid"
        border-width="thin" border-color="#000000">
        <block margin-left="1mm">
          <xsl:value-of select="doc:title" />
        </block>
      </table-cell>
    </table-row>
    <xsl:apply-templates select="pm:usecase" mode="overview"/>
  </xsl:template>
  
  <xsl:template match="pm:usecase" mode="overview">
    <table-row keep-with-previous.within-page="always">
      <table-cell margin-left="0cm" padding="1pt" border-style="solid" border-width="thin">
        <block>
          <basic-link color="blue" internal-destination="{@id}">
            <xsl:value-of select="pm:title" />
          </basic-link>
        </block>
      </table-cell>
      <table-cell margin-left="0cm" padding="1pt" border-style="solid" border-width="thin">
        <block>
          <xsl:value-of select="pm:summary"/>
        </block>
      </table-cell>
      <table-cell margin-left="0cm" padding="1pt" border-style="solid" border-width="thin">
        <block>
          <xsl:apply-templates select="pm:sequence/pm:step" mode="actors"/>
        </block>
      </table-cell>
    </table-row>
  </xsl:template>

  <xsl:template match="pm:step" mode="actors">
    <xsl:variable name="actor" select="substring(@actor, 2)"/>
    <xsl:if test="not(preceding-sibling::pm:step[@actor = current()/@actor])">
      <xsl:if test="position() > 1">, </xsl:if>
      <basic-link color="blue" internal-destination="{$actor}">
        <xsl:value-of select="//pm:actor[@id = $actor]/@name" />
      </basic-link>
    </xsl:if>
  </xsl:template>

  <xsl:template match="pm:usecase">
    <block id="{@id}" color="#228866" font-size="10pt" margin-top="10pt" margin-bottom="6pt">
      <xsl:number level="multiple" count="doc:chapter | pm:usecase" format="1.1" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="pm:title" />
    </block>
    <block margin-left="0.6cm" margin-top="4pt" margin-bottom="4pt" keep-with-previous.within-page="always">
      <xsl:apply-templates select="pm:summary/node()" />
    </block>
    <xsl:apply-templates select="pm:precondition" />
    <xsl:if test="pm:sequence/pm:step">
      <table table-layout="fixed" width="100%" margin-left="0.8cm" margin-top="6pt"
        font-size="9pt" keep-with-previous.within-page="always">
        <table-column column-width="4%" />
        <table-column column-width="18%" />
        <table-column column-width="78%" />
        <table-body>
          <xsl:apply-templates select="pm:sequence/pm:step"/>
        </table-body>
      </table>
    </xsl:if>
    <xsl:apply-templates select="pm:postcondition" />
  </xsl:template>

  <xsl:template match="pm:actor">
    <block id="{@id}" color="#228866" font-size="10pt" margin-top="10pt" margin-bottom="6pt">
      <xsl:number level="multiple" count="doc:chapter | pm:actor" format="1.1" />
      <xsl:text> </xsl:text>
      <xsl:value-of select="@name" />
    </block>
    <block margin-left="0.6cm" margin-top="4pt" margin-bottom="4pt">
      <xsl:value-of select="." />
    </block>
      <!-- xsl:variable name="usecases" select="//usecase[step/@actor = current()/@id]"/>
        <xsl:choose>
        <xsl:when test="count($usecases) = count(//usecase)">
        <p>Ist an allen Use Cases beteiligt.</p>
        </xsl:when>
        <xsl:otherwise>
        <p>Ist an den Use Cases <xsl:apply-templates select="$usecases" mode="array"/> beteiligt.</p>
        </xsl:otherwise>
        </xsl:choose-->
  </xsl:template>

  <xsl:template match="pm:usecase" mode="array">
    <xsl:choose>
      <xsl:when test="(position() = last()) and (position() > 1)"> und </xsl:when>
      <xsl:when test="position() > 1">, </xsl:when>
    </xsl:choose>
    <xsl:value-of select="pm:title" />
  </xsl:template>

  <xsl:template match="pm:step">
    <table-row keep-with-previous.within-page="always">
      <table-cell margin-left="0cm" padding="2pt">
        <block>
          <xsl:value-of select="count(preceding-sibling::pm:step)+1" />.
        </block>
      </table-cell>
      <table-cell margin-left="0cm" padding="2pt">
        <block>
          <xsl:value-of select="//pm:actor[@id = substring(current()/@actor, 2)]/@name" />
        </block>
      </table-cell>
      <table-cell margin-left="0cm" padding="2pt">
        <block>
          <xsl:value-of select="pm:description" />
        </block>
      </table-cell>
    </table-row>
  </xsl:template>

  <xsl:template match="pm:precondition">
    <block margin-left="0.6cm" margin-top="4pt" margin-bottom="4pt" keep-with-previous.within-page="always">
      Precondition:
      <xsl:apply-templates select="pm:val | pm:operand" />
    </block>
  </xsl:template>

  <xsl:template match="pm:postcondition">
    <block margin-left="0.6cm" margin-top="4pt" margin-bottom="4pt" keep-with-previous.within-page="always">
      Postcondition:
      <xsl:apply-templates select="pm:val | pm:operand" />
    </block>
  </xsl:template>

  <xsl:template match="pm:val">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
    <xsl:choose>
      <xsl:when test="@ref">
        <xsl:variable name="ref" select="substring(@ref, 2)"/>
        <basic-link color="blue" internal-destination="{$ref}">
          <xsl:value-of select="//pm:usecase[@id = $ref]/pm:title"/>
        </basic-link>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="."/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

  <xsl:template match="*" mode="operand">
  </xsl:template>
  
  <xsl:template match="pm:operand" mode="operand">
    <xsl:text> </xsl:text>
    <inline font-family="serif"><xsl:value-of select="@type" /></inline>
    <xsl:text> </xsl:text>
  </xsl:template>
  

  <xsl:template match="pm:operand">
    <xsl:if test="position() > 1">
      <xsl:apply-templates select=".." mode="operand"/>
    </xsl:if>
    <xsl:if test="count(../*) > 1">(</xsl:if>
    <xsl:apply-templates select="pm:val | pm:operand"/>
    <xsl:if test="count(../*) > 1">)</xsl:if>
  </xsl:template>

</xsl:stylesheet>
